<?php

function generateid()
	{
	$nrC = 0;
	$chk_code = "";
	$char = "";
	while($nrC <= 9)
		{ 
		$c = rand(48, 90); //generates a casual number between 48 and 90
		if( ($c >= 48) && ($c <= 57) )
			{
			//rand generated a digit
			$char = chr($c);
			$chk_code .= $char;
			$char = "";
			$nrC = $nrC + 1;
			}
		if( ($c >=65) && ($c <= 90) )
			{
			//rand generated a letter
			$char = chr($c);
			$chk_code .= $char;
			$char = "";
			$nrC = $nrC + 1;
			}
		}
	return $chk_code;
	}

//generate a random 10 characters code
$shoppingcart_id = generateid();

//get the current date
$today = date("Y-m-d");

//get the users's ip
$user_ip = $_SERVER['REMOTE_ADDR'];

//now, you can register the shopping cart into the DB
mysql_query("INSERT INTO carts (ID, DATE, IP) VALUES ('$shoppingcart_id' , '$today', '$user_ip')");

//after registering the new cart, set the cookie to the user's browser

//set the lifetime of the cookie
$expire = time()+60*60*24*30; // (1 month lifetime)

//set the domain for which the cookie is valid
$dom = $_SERVER['SERVER_NAME'];
if($dom[0] == "w")
	{
	//truncate the first three characters
	$domain = substr($dom, 3);
	}
else
	{
	//add point
	$domain = ".".$dom;
	}

//finally, send the cookie to the user agent
setcookie("shoppingcart_id", $shoppingcart_id, $expire, "/", $domain);

?>